'''
QLabel与伙伴控件
mainLayout.addWidget(控件名,控件位置行索引,控件位置行索引,控件占几行,控件占几列)
'''
from PyQt5.QtWidgets import *
import sys
from PyQt5.QtGui import QIcon


class QLabelBuddy(QDialog):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        self.setWindowTitle("QLabel与伙伴控件")
        # 创建两个控件
        nameLabel = QLabel('&Name', self)
        nameLineEdit = QLineEdit(self)
        # 设置伙伴控件
        nameLabel.setBuddy(nameLineEdit)

        # 输入密码
        passwordLabel = QLabel('&Password', self)
        passwordLineEdit = QLineEdit(self)
        # 设置伙伴控件
        passwordLabel.setBuddy(passwordLineEdit)
        # 创建按钮
        btnOK = QPushButton("&OK")
        btnCancel = QPushButton("&Cancel")

        # 栅格布局
        mainLayout = QGridLayout(self)
        mainLayout.addWidget(nameLabel, 0, 0)  # (0,0)表示放在一行一列
        mainLayout.addWidget(nameLineEdit, 0, 1, 1, 2)  # (0,1,1,2)表示放在第一行的第二列，占1行两列
        mainLayout.addWidget(passwordLabel, 1, 0)  # (1,0)表示第二行第一列
        mainLayout.addWidget(passwordLineEdit, 1, 1, 1, 2)  # （1,1,1,2)表示在第二行的第二列，占一行两列

        mainLayout.addWidget(btnOK, 2, 1)
        mainLayout.addWidget(btnCancel, 2, 2)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    app.setWindowIcon(QIcon('./img/juice.png'))
    main = QLabelBuddy()
    main.show()
    sys.exit(app.exec_())
# ALT+N,ALT+P
